<?php 
	//负责每日自动结算的自动程序
	//中文编码
	header("Content-type:text/html;charset=utf-8");
	include_once("base.php") ;
	class auto extends  base{
		//准备数据   配置项（比例，增加字段） 数据库  
		function __construct(){
			set_time_limit(0);
			parent::__construct();
			 $configsql="select * from yang_config";
			 $list=$this->select($configsql);
			 foreach ($list as $k=>$v){
			     $list[$v['key']]=$v['value'];
			 }
			$this->config = $list;
		}
		//开始自动程序
		public function run(){
			//echo 1;
			//echo 1;die;
		   //error_reporting( E_ALL&~E_NOTICE );
		    //设置时区
		    date_default_timezone_set('PRC');
		    $this->runGetNewPrice();
			echo "抓完了";
			die;
		}
		//抓数据
		public function runGetNewPrice(){
		     $price5 = $this->config['vic_price'];
		    //比特币
		    $url="https://api.huobi.pro/market/detail/merged?symbol=btcusdt";
		    $result=$this->curl_get($url);
			//dump($result);die;
		    $price1=$result['tick']['close'];
			//dump($price2);die;
		    //以太坊
		    $url1="https://api.huobi.pro/market/detail/merged?symbol=ethusdt";
		    $result1=$this->curl_get($url1);
		    $price2=$result1['tick']['close'];
		    //dump($result1);dump($usd_price3);die;
		    //PAI
		    $url2="https://api.huobi.pro/market/detail/merged?symbol=eosusdt";
		    $result2=$this->curl_get($url2);
		    $price3=$result2['tick']['close'];
			//dump($price1);dump($price2);
		    //处理最新价
		    $sql = "
			UPDATE yang_price SET price1 = ".$price1.",price2=".$price2.",price3=".$price3.",price5=".$price5.",add_time = ".time();
		    //dump($sql);
			$this->query($sql);
    	}		
				//使用curl的GET请求数据  返回数组
		public function curl_get($http){
			//header('Content-Type: multipart/form-data');
			ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)');
		    //初始化
		    $ch = curl_init();
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5000);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_URL, $http);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		    //执行并获取HTML文档内容
		    $output = curl_exec($ch);
			$error = curl_error($ch);
			//dump($error);
		    //释放curl句柄
		    curl_close($ch);
		    //打印获得的数据
		    $result = json_decode($output,true);
		    return $result;
		}
		function curl($url){
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5000);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4'));
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
			$contents = curl_exec($ch);
			$error = curl_error($ch);
			//dump($error);
			curl_close($ch);//关闭一打开的会话
			$contents = json_decode($contents,true);
			return $contents;
		}
	}
	$auto=new auto();
	$auto->run();
?>